<?php

include_once 'Super_Model_Admin.php';
include_once '../entity/admin/AdminGroups.php';

class Admin_Model_AdminGroups extends Super_Model_Admin {

    function getGroups($qAr = NULL, $where = '') {

        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'u')
                ->add('from', 'AdminGroups u')
                ->add('where', 'u.id != 1' . $where);
        if ($qAr)
            $sql->add('orderBy', 'u.' . addslashes($qAr['order_field']) . ' ' . addslashes($qAr['order_dir']));

        $query = $sql->getQuery();
        $result = $query->getArrayResult();
        //$result = $this->getEntityManager()->getRepository( 'Groups' )->findBy(array(), array('id' => 'ASC'), 100, 0);
        if ($result) {
            return $result;
        } else {
            return false;
        }
    }

    function getGroupsById($id) {
        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'u')
                ->add('from', 'AdminGroups u')
                ->add('where', 'u.id = ?1')
                ->setParameter(1, intval($id));
        $query = $sql->getQuery();
        $result = $query->getArrayResult();
        if ($result) {
            return $result[0];
        } else {
            return false;
        }
    }

    function getGroupsNameById($id) {
        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'u.title')
                ->add('from', 'AdminGroups u')
                ->add('where', 'u.id = ?1')
                ->setParameter(1, intval($id));
        $query = $sql->getQuery();
        $result = $query->getArrayResult();
        if ($result) {
            return $result[0]['title'];
        } else {
            return false;
        }
    }

    function getCountGroupsByName($groupName) {
        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'count(u.id)')
                ->add('from', 'AdminGroups u')
                ->add('where', 'u.title = ?1')
                ->setParameter(1, addslashes(trim($groupName)));
        $query = $sql->getQuery();
        $result = $query->getArrayResult();
        if ($result) {
            return $result[0][1];
        } else {
            return false;
        }
    }

    function removeGroup($id) {
        $em = $this->getEntityManager();
        $group = $em->getRepository('AdminGroups')->findOneBy(array('id' => intval($id)));
        if (!empty($group)) {
            $em->remove($group);
            $em->flush();
        }
    }

    function updateGroup($group_id, $perm_acl, $perm_view) {
        $em = $this->getEntityManager();
        $group = $em->getRepository('AdminGroups')->findOneBy(array('id' => intval($group_id)));
        if (!empty($group)) {
            $group->perm_acl = $perm_acl;
            $group->perm_view = $perm_view;
            $em->persist($group);
            $em->flush();
        }
    }

    function insertGroup($groupName, $copy_group_id) {
        $em = $this->getEntityManager();
        $group = new AdminGroups();
        $group->title = addslashes(trim($groupName));
        if ($copy_group_id) {
            $copy_group_info = $this->getGroupsById($copy_group_id);
            if ($copy_group_info) {
                $group->perm_view = $copy_group_info['perm_view'];
                $group->perm_acl = $copy_group_info['perm_acl'];
            }
        }
        $em->persist($group);
        $em->flush();
    }

    function getRole() {
        $sql = $this->getEntityManager()->createQueryBuilder();
        $sql->add('select', 'u')
                ->add('from', 'AdminGroups u');
        $sql->add('orderBy', 'u.title');

        $query = $sql->getQuery();

        return $query->getArrayResult();
    }

    public function getGroupPermissions($group_id) {
        $perm = $this->getGroupsById($group_id);

        $result = array();
        if (!empty($perm)) {
            $permView = json_decode($perm['perm_view']);
            if (is_array($permView) && count($permView)) {

                foreach ($permView as $pv) {
                    $result[] = array(0 => 'controller-action', 1 => $pv);
                }
            }
            return array('all_perm' => array_merge($result, json_decode($perm['perm_acl'])), 'perm_view' => $permView);
        }
        return false;
    }

    public function getBasePermissions() {
        $perm = $this->getEntityManager()->getRepository('AdminGroups')->findOneBy(array('id' => 1));
        if ($perm) {
            return $perm->perm_acl;
        }
        return false;
    }

    public function getAdminViewPermissions() {
        $permissions_all = json_decode($this->getBasePermissions(), true);
        $result = array();
        if (is_array($permissions_all) && count($permissions_all)) {
            foreach ($permissions_all as $perm_modules) {
                if (!empty($perm_modules['modules']))
                    foreach ($perm_modules['modules'] as $m_name => $perm) {
                        if (!empty($perm['permissions']))
                            foreach ($perm['permissions'] as $p_name => $pr) {
                                $result[] = $m_name . '_' . $p_name;
                            }
                    }
            }
        }
        return $result;
    }

    public function getPermissionsArray($permission) {
        $result = array();
        if (is_array($permission) && count($permission)) {
            foreach ($permission as $perm) {
                $result[] = $perm[0] . '|' . $perm[1];
            }
        }
        return $result;
    }

    public function updateBasePermissions(array $permissions) {
        $em = $this->getEntityManager();
        $group = $em->getRepository('AdminGroups')->findOneBy(array('id' => 1));
        if (!empty($group)) {
            $group->perm_acl = json_encode($permissions);
            $em->persist($group);
            $em->flush();
        }
    }

    public function addNewRulesToGroups($sSectionName, $aNewRules) {
        $sSectionName = str_replace('-', '_', $sSectionName);
        $aGroups = $this->getGroups(NULL, ' AND u.perm_view LIKE \'%' . $sSectionName . '%\'');
        if (!empty($aNewRules)) {
            if (count($aGroups)) {
                $em = $this->getEntityManager();
                foreach ($aGroups as $aGrp) {
                    $oGroup = $em->getRepository('AdminGroups')->findOneBy(array('id' => intval($aGrp['id'])));
                    if (!empty($oGroup)) {
                        $aPermAcl = json_decode($oGroup->perm_acl, 1);
                        foreach ($aNewRules as $aNRule) {
                            $aPermAcl[] = $aNRule;
                        }
                        $oGroup->perm_acl = json_encode($aPermAcl);
                        $em->persist($oGroup);
                        $em->flush();
                    }
                }
            }
        }
    }

}